package com.hongning.railway.ui.main.fragments.college;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.ActivityAboutBinding;
import com.hongning.railway.databinding.FragemntCollegefileterBinding;
import com.hongning.railway.entity.resbean.Fileterbean;
import com.hongning.railway.entity.resbean.ReqFilterBean;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.CollegeFilterAdapter;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.CollegeFilterItemAdapter;
import com.hongning.railway.ui.main.mine.partymember.QuestionFragmentViewModel;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class CollegeFilterFragment extends BaseFragment<FragemntCollegefileterBinding, CollegeFilterViewModel> {
    CollegeFilterAdapter adapter1;
     CollegeFilterItemAdapter collegeFilterItemAdapter;
    @Override
    public void initData() {
        super.initData();
        binding.appTitle.tvTopTitle.setText("筛选");
        binding.appTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.appTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
     binding.collegefileterRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),4));
        collegeFilterItemAdapter=new CollegeFilterItemAdapter(initFirstDatas());
        binding.collegefileterRecyclerView.setAdapter(collegeFilterItemAdapter);
        collegeFilterItemAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);

       binding.collegefileterRecyclerView1.setLayoutManager(new LinearLayoutManager(getActivity()));
       adapter1=new CollegeFilterAdapter(new ArrayList<>());
        binding.collegefileterRecyclerView1.setAdapter(adapter1);
        adapter1.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        binding.collegefileterCz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重置
                collegeFilterItemAdapter.setSelectIndex(-1);
                adapter1.setNewData(setDefaultfileterbeans());
//                ToastUtils.showShort("清除成功");
            }
        });
        binding.collegefileterQd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确定
                ReqFilterBean reqFilterBean=new ReqFilterBean();
                if (collegeFilterItemAdapter.getSelectIndex()>-1)
                {
                    reqFilterBean.setNewsCategory(collegeFilterItemAdapter.getItem(collegeFilterItemAdapter.getSelectIndex()).getNewsCategory());
                }
                reqFilterBean.setNewsSubTypeId(getNewsSubTypeId());
                Messenger.getDefault().send(reqFilterBean, Constant.filter_code);
                getActivity().finish();
            }
        });
    }

    public  List<Fileterbean> setDefaultfileterbeans(){
        if (adapter1.getItemCount()>0)
        {
            for (Fileterbean fileterbean:adapter1.getData())
            {
                if (fileterbean.getChildData()!=null&&!fileterbean.getChildData().isEmpty())
                {
                    for (Fileterbean.ChildDataBean childDataBean:fileterbean.getChildData())
                    {
                    childDataBean.setSelect(false);
                    }

                }            }
        }
        return adapter1.getData();
    }
    /**
     * 得到选中的id
     * @return
     */
    public String getNewsSubTypeId(){
        String tem=null;
        if (adapter1.getItemCount()>0)
        {
            for (Fileterbean fileterbean:adapter1.getData())
            {
                if (fileterbean.getChildData()!=null&&!fileterbean.getChildData().isEmpty())
                {
                    for (Fileterbean.ChildDataBean childDataBean:fileterbean.getChildData())
                    {
                        if (childDataBean.isSelect())
                        {
                            if (tem==null)
                            {
                                tem="";
                                tem+=childDataBean.getTitleid();
                            }else {
                                tem+=(","+childDataBean.getTitleid());
                            }
                        }
                    }

                }            }
        }
        return tem;
    }

    public List<Fileterbean.ChildDataBean> initFirstDatas(){
        List<Fileterbean.ChildDataBean> childDataBeans=new ArrayList<>();
        childDataBeans.add(new Fileterbean.ChildDataBean("视频","001","2"));
        childDataBeans.add(new Fileterbean.ChildDataBean("图文","002","1"));
        childDataBeans.add(new Fileterbean.ChildDataBean("链接","003","3"));

        return childDataBeans;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.dialogUIChangeObserable.fileterBeans.observe(this, new Observer<List<Fileterbean>>() {
            @Override
            public void onChanged(@Nullable List<Fileterbean> fileterbeans) {
                adapter1.setNewData(fileterbeans);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.init();
    }

    @Override
    public CollegeFilterViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(CollegeFilterViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragemnt_collegefileter;
    }

    @Override
    public int initVariableId() {
        return BR.myviewModel;
    }
}
